//
//  MSEmptyView.m
//  MSJD
//
//  Created by 箭军刘 on 2018/10/19.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MSEmptyView.h"

@interface MSEmptyView()

@property (nonatomic, strong) UILabel *toastLabel; // 提示标签
@property (nonatomic, strong) UIButton *retryButton; // 重试按钮

@property (nonatomic, assign) MSEmptyType emptyType;
@property (nonatomic, copy) NSString *toast;

@end

@implementation MSEmptyView

+ (instancetype)loadEmptyViewWithType:(MSEmptyType)emptyType toast:(NSString *)toast frame:(CGRect)frame {
    MSEmptyView *emptyView = [[MSEmptyView alloc] initWithFrame:frame type:emptyType toast:toast];
    emptyView.emptyType = emptyType;
    emptyView.toast = toast;
    return emptyView;
}

- (instancetype)initWithFrame:(CGRect)frame type:(MSEmptyType)type toast:(NSString *)toast {
    if (self = [super initWithFrame:frame]) {
    
        self.backgroundColor = [UIColor clearColor];
        _emptyType = type;
        _toast = toast;
        [self initSubViews];
    }
    return self;
}

#pragma mark - Init Method

- (void)initSubViews {
    
    // 提示控件
    CGFloat toastX = 20;
    CGFloat toastWidth = MS_SCREEN_WIDTH - 2 * toastX;
    CGFloat toastHeight = 24;
    CGFloat toastCenterY = (self.emptyType == MSEmptyTypeToast)?0:40;
    CGFloat toastTop = (MS_SCREEN_HEIGHT / 2.0) - toastCenterY - toastHeight;
    UILabel *toastLabel = [[UILabel alloc] initWithFrame:CGRectMake(toastX, toastTop, toastWidth, toastHeight)];
    toastLabel.text = [MSSafeObject stringInfo:self.toast];
    toastLabel.textColor = [UIColor blackColor];
    toastLabel.font = [UIFont systemFontOfSize:16];
    toastLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:toastLabel];
    self.toastLabel = toastLabel;
    
    if (self.emptyType == MSEmptyTypeToast) {
        return;
    }
    // 重试按钮
    CGFloat retryBtnWidth = 150;
    CGFloat retryBtnHeight = 45;
    CGFloat retryBtnX = (MS_SCREEN_WIDTH - retryBtnWidth) / 2.0;
    CGFloat retryBtnY = (MS_SCREEN_HEIGHT / 2.0) - 20;
    UIButton *retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [retryButton setFrame:CGRectMake(retryBtnX, retryBtnY, retryBtnWidth, retryBtnHeight)];
    [retryButton setBackgroundColor:MSThemeColor];
    NSString *tip = (self.emptyType == MSEmptyTypeLoginTip)?@"点击登录":@"点击重试";
    [retryButton setTitle:tip forState:UIControlStateNormal];
    [retryButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [retryButton addTarget:self action:@selector(retryButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    retryButton.layer.cornerRadius = retryBtnHeight / 2.0;
    retryButton.layer.masksToBounds = YES;
    [self addSubview:retryButton];
    self.retryButton = retryButton;
}

- (void)resetRetryButtonStatus {
    self.retryButton.enabled = YES;
}

#pragma mark - Event Response

- (void)retryButtonClick:(UIButton *)sender {
    sender.enabled = NO;
    if (self.RetryButtonClickBlock) {
        self.RetryButtonClickBlock();
    }
}

#pragma mark -  Memory Method
- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
